package com.example.demo.Gmail.Excutor;


import com.example.demo.Gmail.Handle.GmailHandle;
import com.example.demo.Gmail.Handle.PartsHandle;
import com.example.demo.Gmail.Handle.proxyHandler;
import com.example.demo.Gmail.Util.OSSUtils;
import com.example.demo.Gmail.model.AttachMentIdModel;
import com.example.demo.Gmail.model.AttchmentModel;
import com.example.demo.Gmail.model.MessageModel;
import com.example.demo.Gmail.model.UserModel;
import com.google.api.client.util.Base64;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.Message;
import com.google.api.services.gmail.model.MessagePartBody;
import com.google.api.services.gmail.model.MessagePartHeader;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;

public class ThreadUpload implements Callable {
    private MessageModel messageModel;
    private final String URL="http://download.files.cnexps.com/";
    private String lable;
    private String id;
    private UserModel user;
    public ThreadUpload(String lable, UserModel user,String id){
       this.lable=lable;
       this.user=user;
       this.id=id;
    }

    @Override
    public MessageModel call() throws Exception {
        //大陆环境需要加代理
        System.out.println(id);
        proxyHandler.proxy();
        PartsHandle partsHandle=new PartsHandle();
        Gmail service= GmailHandle.service(user);
        Message message= service.users().messages().get("me",id).execute();
        messageModel=new MessageModel();
        List list= Lists.newArrayList();
        list.add(message.getPayload());
        List<AttachMentIdModel> attachMentIdModels= partsHandle.recurrenceGetData(list);
        List<AttchmentModel> attchmentModels=Lists.newArrayList();
        for (AttachMentIdModel attachMentIdModel:attachMentIdModels){
            //正文部分内容，不上传，保存到本地数据库
            if (attachMentIdModel.getData()!=null){
                if(attachMentIdModel.getMedieType().equals("text/plain")){
                    byte[] fileByteArray = Base64.decodeBase64(attachMentIdModel.getData());
                    String text=new String(fileByteArray);
                    messageModel.setPlain(text);
                }else if(attachMentIdModel.getMedieType().equals("text/html")){
                    byte[] fileByteArray = Base64.decodeBase64(attachMentIdModel.getData());
                    String text=new String(fileByteArray);
                    messageModel.setHtml(text);
                }
            }else {
                AttchmentModel attchmentModel=new AttchmentModel();
                MessagePartBody attachPart=  service.users().messages()
                        .attachments()
                        .get("me",message.getId(),attachMentIdModel.getAttchMentId())
                        .execute();
                byte[] fileByteArray = Base64.decodeBase64(attachPart.getData());
                String fileName=   UUID.randomUUID().toString()+"/"+attachMentIdModel.getFileName();
                String path = OSSUtils.getDefaultOSS().upLoadFileToOss(fileByteArray, fileName);
                attchmentModel.setFileName(attachMentIdModel.getFileName());
                attchmentModel.setUrl(URL+path);
                attchmentModels.add(attchmentModel);
            }
        }
        List<MessagePartHeader> headers=message.getPayload().getHeaders();
        for (MessagePartHeader header:headers){
            if (header.getName().equals("Cc")){
                messageModel.setCc(header.getValue());
            }else if (header.getName().equals("To")){
                messageModel.setTo(header.getValue());
            }else if (header.getName().equals("From")){
                messageModel.setFrom(header.getValue());
            }else if (header.getName().equals("Subject")){
                messageModel.setSubject(header.getValue());
            }
        }
        messageModel.setLabel(lable);
        messageModel.setMessageId(message.getId());
        messageModel.setAttachMentModels(attchmentModels);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(message.getInternalDate().toString());
        Date date = new Date(lt);
        messageModel.setIntnetdata(simpleDateFormat.format(date));
        messageModel.setStatus(1);
        messageModel.setUserName(user.getUserName());
        messageModel.setEmailAddress(user.getEmailAddress());
        return messageModel;
    }
}
